import axios from 'axios'
import cryptoJS from 'crypto-js'
const urlPath = `http://${process.env.HOST || '127.0.0.1'}:${process.env.PORT || '3000'}`

export const state = () => ({
  user: {},
  position: {},
  hotPlace: [],
  menu: []
})
export const mutations = {
  SET_USER: (state, user) => {
    state.user = user
  },
  SET_POSITION: (state, position) => {
    position.city = position.city ? position.city.replace(/市/, '') : ''
    state.position = position
  },
  SET_HOTPLACE: (state, hotPlace) => {
    state.hotPlace = hotPlace
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  }
}

export const actions = {
  async signup({commit}, {username, password, email, code}) {
    let res = await axios.post(`${urlPath}/users/signup`, {
      username: encodeURIComponent(username),
      password: CryptoJS.MD5(password).toString(),
      email: email,
      code: code
    })
  },
  async signin({commit}, {username, password}) {
    let res = await axios.post(`${urlPath}/users/signin`, {
      username: encodeURIComponent(username),
      password: cryptoJS.MD5(password).toString()
    })
    return res
  },
  async verify({commit}, {username, email}) {
    let res = await axios.post(`${urlPath}/users/verify`, {
      username: encodeURIComponent(username),
      email
    })
    return res
  },
  async getUser({commit}) {
    let res = await axios.get(`${urlPath}/users/getUser`)
    if (res.status === 200 && res.data && res.data.code === 0) {
      let { user, email } = res.data
      commit('SET_USER', {
        username: user,
        email
      })
    }
    return res
  },
  async exit({commit}) {
    let res = await axios.get(`${urlPath}/users/exit`)
    return res
  },
  async getMenu({commit}) {
    let res = await axios.get(`${urlPath}/geo/menu`)
    if (res.status === 200 && res.data && res.data.code === 0) {
      commit('SET_MENU', res.data.menu)
    }
    return res
  },
  async getPosition({commit}) {
    let {status, data} = await axios.get(`${urlPath}/geo/getPosition`)
    if (status === 200 && data.province && data.city) {
      commit('SET_POSITION',{
        province: data.province,
        city: data.city
      })
    }
    return data
  },
  async searchTop({commit}, top) {
    let res = await axios.get(`${urlPath}/search/top`, { params: top })
    return res
  },
  async hotPlace({commit}, city) {
    let {status, data} = await axios.get(`${urlPath}/search/hotPlace`,{ params: {
         city
      }
    })
    if (status === 200 && data.hotPlace) {
      commit('SET_HOTPLACE', data.hotPlace)
    }
    return data
  },
  async searchByKeywords({commit}, {city, keyword}) {
    let res = await axios.get(`${urlPath}/search/searchByKeywords`,{ params: {
        city,
        keyword
      }
    })
    return res
  },
  async getProvince({commit}) {
    let res = await axios.get(`${urlPath}/geo/province`)
    return res
  },
  async getCity({commit}, city) {
    let res = await axios.get(`${urlPath}/geo/area`, { params: { city } })
    return res
  },

  async getCitys({commit}, id) {
    let res = await axios.get(`${urlPath}/geo/province/${id}`)
    return res
  },

  async getAllCitys({commit}) {
    let res = await axios.get(`${urlPath}/geo/city`)
    return res
  },
  async searchByKeywords({commit}, {city, keyword}) {
    let res = await axios.get(`${urlPath}/search/searchByKeywords`, { params: { city, keyword } })
    return res
  },

  async product({commit}, arg) {
    let res = await axios.get(`${urlPath}/search/product`, { params: arg })
    
    return res
  },
  
}
